﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using Pixysoft.Reflection;
using System.Diagnostics;

namespace Pixysoft.Framework.Verifications
{
    class Class1
    {
        public void test001()
        {
            try
            {
                Method1();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            try
            {
                Method2();
            }
            catch (Exception ex)
            {
                if (Exceptions.IsHandled(ex))
                    return;

                Console.WriteLine("error here.");
            }
        }

        public void test002()
        {
            MethodInfo method = typeof(Class1).GetMethod("Method2");

            foreach (ExceptionHandlingClause ex in method.GetMethodBody().ExceptionHandlingClauses)
            {
            }

            foreach (ILInstruction il in new Pixysoft.Reflection.MethodBodyReflector(method).Instructions)
            {
                Console.WriteLine(il.ToString());
            }
        }

        public void test003()
        {
            try
            {
                string name = null;

                throw Exceptions.VerificationFailedException(name);
            }
            catch
            {
            }
        }

        public void test004()
        {
            Console.WriteLine(
                Verification.Verifies( new int[] { 2, 3 }).IsGreater(1).Result());
        }

        public void Method1()
        {
            string parmeter = getparameter();

            if (string.IsNullOrEmpty(parmeter))
                throw Exceptions.UnexpectedResultException(parmeter);
        }

        public void Method2()
        {
            try
            {
                hello(null);
            }
            catch (Exception ex)
            {
                if (Exceptions.IsHandled(ex))
                    throw ex;

                Console.WriteLine("write down something in method2");

                throw Exceptions.Parse(ex).Handle();
            }
        }

        public void Method3()
        {
            throw new Exception("123");
        }

        private void hello(string hello)
        {
            hello = hello.Trim().ToUpper();
        }

        private string getparameter()
        {
            return null;
        }
    }
}
